﻿function New-TSMNode {
    <#
        .SYNOPSIS
            Define new TSM node
                    
        .DESCRIPTION
            The New-TSMNode registers a new TSM node
        
        .EXAMPLE
            New-TSMNode "nodename" "password" Domain="somedomain"

        .NOTES
            Contributors:  David Seibel
            Last Revision Date: 09/02/2015
    #>
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0, Mandatory = $true)]
        $Name,
        
        [Parameter(Position = 1)]
        $Password,
        
        [Parameter(Position = 2, Mandatory = $true)]
        $Domain,
        
        [ValidateRange(0,9999)]
        [Int32]$PasswordExpirationDays,

        $UserID = "NONE",

        $Contact,        
        
        [ValidateSet("Yes","No")]
        $CanDeleteArchive = "No",
        
        [ValidateSet("Yes","No")]
        $CanDeleteBackup = "No",

        [ValidateSet("Yes","No")]
        $ForcePasswordReset = "No",
        
        [ValidateSet("Client","NAS","Server")]
        $Type = "Client",

        $URL,

        [ValidatePattern("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b")]
        $EmailAddress,
        
        [ValidateSet("Enabled","Disabled")]
        $ReplicationState = "Enabled",
        
        [ValidateSet("Local","LDAP")]
        $Authentication = "Local",
        
        [ValidateSet("ServerOnly","ClientOrServer")]
        $Deduplication = "ServerOnly",
        
        [ValidateSet("Yes","No","Default","ServerOnly")]
        $SSLRequired = "Default"
    )

    if ($Authentication -eq "Local" -and (($Password -eq $null) -or ($Password -eq ""))) {
        throw "Password is required when using local authentication!"
    }
    $command = "Register Node $Name $Password " `
             + "PassExp=$PasswordExpirationDays " `
             + "UserID=$UserID " `
             + "Contact='$Contact' " `
             + "Domain=$Domain " `
             + "ArchDelete=$CanDeleteArchive " `
             + "BackDelete=$CanDeleteBackup " `
             + "ForcePWReset=$ForcePasswordReset " `
             + "Type=$Type " `
             + "URL='$URL' " `
             + "EmailAddress='$Email' " `
             + "ReplState=$ReplicationState " `
             + "Deduplication=$Deduplication " `
             + "Authentication=$Authentication"

    $output = Invoke-TSMCommand -Command $command
    return $output
}